<template>
  <div class="home">
    <el-container>
    <el-header>
      <div class="left">
        <img src="../assets/heima.png" alt="">
        <span>杭州前端12期后台管理系统</span>
      </div>
      <el-button @click="logout">退出</el-button>
    </el-header>
    <el-container>
        <el-aside :width="flag?'64px':'200px'">
          <div class="toggleMenu" @click="flag=!flag">|||</div>
          <el-menu
            router
            :collapse="flag"
            :collapse-transition="false"
            :default-active='activeDefault'
            unique-opened
            class="el-menu-vertical-demo"
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409eff">
            <el-submenu v-for="item in menus" :key="item.id" :index="item.id+''">
              <template slot="title">
              <i :class="iconsList[item.id]"></i>
              <span>{{item.authName}}</span>
              </template>
              <el-menu-item v-for="item2 in item.children" :key="item2.id" :index="'/'+item2.path">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{item2.authName}}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
    </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data () {
    return {
      flag: false,
      menus: [],
      activeDefault: '',
      iconsList: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },

  created () {
    this.getMenus()
    this.activeDefault = this.$route.path
  },

  methods: {
    logout () {
      sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    async getMenus () {
      const { data: res } = await this.$axios.get('/menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取数据失败')
      this.menus = res.data
    }
  }
}
</script>

<style scoped lang='less'>
.home,.el-container{
  height: 100%;
}
.el-header{
  background: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .left{
    display: flex;
    align-items: center;
    img{
      width: 50px;
      height: 50px;
    }
    span{
      font-size: 25px;
      color: #fff;
      margin-left: 15px;
    }
  }
}
.el-aside{
  background: #333744;
  .iconfont {
  margin-right: 10px;
  }
  .toggleMenu{
    width: 100%;
    height: 40px;
    background: #666;
    color: #fff;
    line-height: 40px;
    text-align: center;
    letter-spacing: 5px;
    cursor: pointer;
    user-select:none
  }
}
.el-main{
  background: #eaedf1;
}

.el-menu{
  border-right:none;
}
</style>
